package com.bawei.park2302.park

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_base.utils.utils.entity.ParkBeanItem
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.lib_base.utils.utils.utils.Cons
import com.bawei.park2302.R
import com.bawei.park2302.databinding.ActivityParkAddBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/ParkAddActivity")
@AndroidEntryPoint
class ParkAddActivity : BaseActivity<ActivityParkAddBinding,ParkIntent,ParkViewModel>() {
    override val binding: ActivityParkAddBinding by lazy {
        ActivityParkAddBinding.inflate(layoutInflater)
    }
    override val vm: ParkViewModel by viewModels<ParkViewModel>()

    var area = "A园区C栋"
    var type = "标准车位"
    var status = "空闲"
    override fun initView() {
        binding.parkAddRemark.addTextChangedListener {object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                val length = p0?.length
                binding.parkAddNum.text = "${length}/10"
                if (length == 10){
                    binding.parkAddRemark.setTextColor(Color.RED)
                }else{
                    binding.parkAddRemark.setTextColor(Color.DKGRAY)
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        }}

        val areaList = arrayOf("A区停车场", "B区停车场","C区停车场")
        val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areaList)
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkAddArea.adapter = arrayAdapter

        binding.parkAddArea.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                area = areaList[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }
        }

        val typeList = arrayOf("标准车位", "VIP车位", "至尊车位")
        val typeAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, typeList)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkAddType.adapter = typeAdapter

        binding.parkAddType.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                type = typeList[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }
        }

        binding.parkAddRg.setOnCheckedChangeListener { radioGroup, checkedId ->
            if (checkedId == R.id.rb_kx){
                status = "空闲"
            }else if (checkedId == R.id.rb_zy){
                status = "已占用"
            }else{
                status = "维护中"
            }
        }

        binding.ivSubmit.setOnClickListener {
            val carNo = binding.parkAddCarNo.text.toString()
            val remark = binding.parkAddRemark.text.toString()
            val plate = binding.parkAddPlate.text.toString()
            val username = binding.parkAddUserName.text.toString()
            val parkBeanItem = ParkBeanItem(
                1,
                area,
                1,
                plate,
                remark,
                carNo,
                status,
                type,
                SPUtils.getInstance().getInt(Cons.UID),
                username
            )
            vm.sendIntent(ParkIntent.parkAdd(parkBeanItem))
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> -> {
                        ToastUtils.showLong("添加成功")
                        finish()
                    }
                    else -> {}
                }
            }
        }
    }
}